<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            @change="changePlant(searchParam.plantId)"
            v-model="searchParam.plantId"
            clearable
            placeholder="请选择"
            filterable
          >
            <el-option
              v-for="item in plants"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="呼叫类型">
          <el-select
            v-model="searchParam.callTypeId"
            placeholder="请选择"
            clearable
            filterable
          >
            <el-option
              v-for="item in callTypes"
              :key="item.id"
              :label="item.callTypeName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="呼叫描述">
          <el-input
            v-model="searchParam.callSettingName"
            placeholder="请输入呼叫描述"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="searchParam.enabled" clearable>
            <el-option
              v-for="item in useDictionary('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="呼叫编号"
          prop="callSettingCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="呼叫描述"
          prop="callSettingName"
          min-width="300"
          show-overflow-tooltip
        />
        <el-table-column
          label="呼叫类型"
          prop="callTypeName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column-->
        <!--          label="班组"-->
        <!--          prop="crewCode"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <el-table-column
          label="是否呼叫"
          prop="callMethod"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.callMethod === 1">是</el-tag>
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否呼叫休班人"
          prop="callOffDuty"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.callOffDuty === 1"
              >是</el-tag
            >
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template #default="scope">
            <el-dropdown size="small">
              <el-button type="primary" size="small" style="width: 75px">
                更多菜单
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="openDialogdetail(scope.row)"
                    >查看</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDialog(scope.row)"
                    >编辑</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDialogMemberSetting(scope.row)"
                    >班组角色</el-dropdown-item
                  >
                  <el-dropdown-item @click="openAndCallAreaDialog(scope.row)"
                    >工作中心</el-dropdown-item
                  >
                  <el-dropdown-item @click="openDetailDialog(scope.row)"
                    >呼叫升级设定</el-dropdown-item
                  >
                  <el-dropdown-item @click="handleTableDelete(scope.row.id)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <andcallsetting-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <andcallmembersetting
      :visible="andcallmembersettingDialog.visible"
      :title="andcallmembersettingDialog.title"
      :data="andcallmembersettingDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="andcallmembersettingDialog.readvisible"
      @change-visible="changeVisible"
    />
    <andcallareasetting
      :visible="andcallareasettingDialog.visible"
      :title="andcallareasettingDialog.title"
      :data="andcallareasettingDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="andcallareasettingDialog.readvisible"
      @change-visible="changeVisible"
    />

    <andcallsetting-detail
      :visible="detailDialog.visible"
      :title="detailDialog.title"
      :data="detailDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="detailDialog.readvisible"
      @change-visible="changeDetailVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "ANDCALLSETTING",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/andon/andcallsetting/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};
let plants = ref([]);
let callTypes = ref([]);
// 初始化搜索参数数据
const searchParamInit = {
  callSettingName: "",
  callTypeId: "",
  plantId: "",
  enabled: "",
};
function changePlant(id) {
  getCallTypes(id);
}
async function getCallTypes(id) {
  const data = await request({
    url: "/andon/andcalltype/querycalltypes",
    method: "post",
    data: { plantId: id },
  });
  callTypes.value = data.data;
}
const deleteParam = {
  api: "/andon/andcallsetting/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行
// function getSelectIds(ids) {
//   console.log("123456", ids);
//   searchParam.organizationId = ids;
//   getPlants();
// }
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
  searchParam.value.plantId = plants.value[0].id;
  changePlant(searchParam.plantId);
}
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlants();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

const detailDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
const andcallmembersettingDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
const andcallareasettingDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/andon/andcallsetting/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
}

async function getInfoDetail(id: string) {
  const { data } = await request({
    url: `/andon/andcallsetting/get/${id}`,
    method: "get",
  });
  detailDialog.data = data;
  detailDialog.readvisible = true;
  detailDialog.visible = true;
}

/** 打开安东呼叫设定表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改安东呼叫设定";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增安东呼叫设定";
  }
  addDialog.readvisible = false;
  addDialog.visible = true;
}

/** 打开安东呼叫设定表单弹窗 */
function openDetailDialog(roleObj?: Object) {
  detailDialog.title = "安东呼叫升级设定";
  getInfoDetail(roleObj.id);
}

/** 打开安东呼叫设定表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "安东呼叫设定详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.visible = true;
}
/** 打开安东呼叫设定表单弹窗 详情 */
function openAndCallAreaDialog(roleObj?: any) {
  console.log("roleObj", roleObj);
  andcallareasettingDialog.title = "工作中心设定";
  andcallareasettingDialog.data = roleObj;
  andcallareasettingDialog.readvisible = false;
  andcallareasettingDialog.visible = true;
}
/** 打开安东呼叫设定表单弹窗 详情 */
function openDialogMemberSetting(roleObj?: any) {
  console.log("roleObj", roleObj);
  andcallmembersettingDialog.title = "班组角色设定";
  andcallmembersettingDialog.data = roleObj;
  andcallmembersettingDialog.readvisible = false;
  andcallmembersettingDialog.visible = true;
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  andcallmembersettingDialog.visible = value;
  andcallareasettingDialog.visible = value;
  if (!value) {
    addDialog.data = {};
    andcallmembersettingDialog.data = {};
    andcallareasettingDialog.data = {};
  }
}

function changeDetailVisible(value: boolean) {
  detailDialog.visible = value;
  if (!value) {
    detailDialog.data = {};
  }
}
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 63px;
}
</style>
